﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.Base;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForCustInserting : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //销售订单
            UFIDA.U9.CBO.SCM.Customer.Customer cust = key.GetEntity() as UFIDA.U9.CBO.SCM.Customer.Customer;
            if (cust == null) return;

            #region 客户保存获取id

            foreach (var line in cust.CustomerSites)
            {
                using (ISession session = Session.Open())
                {
                    //多个位置
                    if (line.IsPayment!=true)
                    {
                    
                        string sql = $@"update line set line.BillToSite = ls.ID, 
                                        line.IsBillTo = 0 ,
                                        line.ShippmentRule = cust.ShippmentRule,
                                        line.ARConfirmTerm = cust.ARConfirmTerm,
                                        line.TaxSchedule = cust.TaxSchedule,
                                        line.TradeCurrency = cust.TradeCurrency
                                        from CBO_CustomerSite line 
                                        left join CBO_Customer cust on cust.ID = line.Customer
                                        join (select line.id ID,line.code Code   from CBO_Customer cust 
                                        left join CBO_CustomerSite line on line.Customer  = cust.ID 
                                        where line.IsPayment = 1) ls on ls.Code = cust.Code 
                                        where cust.Code =  '{cust.Code}' and cust.Org = '{Context.LoginOrg.ID}'  and line.IsPayment = 0 ";
                        DataSet ds = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);


                        string sql1 = $@"UPDATE CBO_Customer set IsTaxPrice  = 1
                                    from CBO_Customer 
                                where Code = '{cust.Code}' and Org = '{Context.LoginOrg.ID}';";
                        DataSet ds1 = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);
                    }
                //一个位置
                    else {
                        string sql = $@"update line set 
                                line.ShippmentRule = cust.ShippmentRule,
								line.ARConfirmTerm = cust.ARConfirmTerm,
								line.TaxSchedule = cust.TaxSchedule,
								line.TradeCurrency = cust.TradeCurrency
                                from CBO_CustomerSite line 
                                left join CBO_Customer cust on cust.ID = line.Customer
                                where cust.Code = '{cust.Code}' and cust.Org = '{Context.LoginOrg.ID}' and line.IsPayment = 1;";
                        DataSet ds = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);


                        string sql1 = $@"UPDATE CBO_Customer set IsTaxPrice  = 1
                                    from CBO_Customer 
                                where Code = '{cust.Code}' and Org = '{Context.LoginOrg.ID}';";
                        DataSet ds1 = null;
                        DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);

                        }
                    session.Commit();
                }
              
                    
      
            }



            #endregion

        }

    }
}
